﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FirmaTransportowa
{
    public class ManagerPanelPresenter
    {
        private ManagerPanelForm managerPanelView;
        private ManagerAddSalaryForm managerAddSalaryView;
        private ManagerSalariesListForm managerSalariesListView;

        private User user;

        public ManagerPanelPresenter(User user) {
            managerPanelView = new ManagerPanelForm(this);
            this.user = user;
            showLoggedUserName();
            managerPanelView.Show();
        }

        public void showAddNewSalaryView() {
            if (managerAddSalaryView == null || managerAddSalaryView.IsDisposed)
            {
                managerAddSalaryView = new ManagerAddSalaryForm(this);
                managerAddSalaryView.Show();
                showActiveWorkers();
                managerAddSalaryView.showDataTable();
            }
        }

        public void showSalariesListView() {
            if (managerSalariesListView == null || managerSalariesListView.IsDisposed)
            {
                managerSalariesListView = new ManagerSalariesListForm(this);
                managerSalariesListView.Show();
                showSalariesList();
                managerSalariesListView.showDataTable();
            }
        }

        private void showActiveWorkers() {
            managerAddSalaryView.activeWorkers.DataSource = Worker.getWorkers();
        }

        private void showSalariesList() {
            managerSalariesListView.salariesList.DataSource = Salary.getSalariesForManager();
        }

        public void addSalary() {
            String error = "";

            float number = 0.0f;
            if (managerAddSalaryView.amountInput.Text.Trim() != "")
            {
                string amount = managerAddSalaryView.amountInput.Text.Trim().Replace('.', ',');

                number = float.Parse(amount);
                if (float.IsNaN(number))
                {
                    error = "Podana kwota jest nieprawidłowa!";
                }
            }
            else
            {
                error = "Podaj kwotę!";
            }

            int workerId = 0;
            int occupationId = 0;
            int selectedWorkers = 0;
            foreach (DataGridViewRow row in managerAddSalaryView.activeWorkers.SelectedRows)
            {
                selectedWorkers++;
                if (selectedWorkers == 1) {
                    workerId = int.Parse(row.Cells[0].Value.ToString());
                    occupationId = int.Parse(row.Cells[5].Value.ToString());
                }
            }

            if (selectedWorkers > 1) {
                error = "Wybierz tylko jednego pracownika";
            }

            if (selectedWorkers == 0) {
                error = "Wybierz pracownika!";
            }

            if (error == "")
            {
                Salary salary = new Salary();

                salary.payment_date = DateTime.Parse(managerAddSalaryView.datePicker.Text);
                salary.amount = number;

                salary.worker_id = workerId;
                salary.occupation_id = occupationId;
                salary.save();
                MessageBox.Show("Dodano wypłatę");
                managerAddSalaryView.Dispose();
            }
            else 
            {
                MessageBox.Show(error);
            }
        }

        private void showLoggedUserName()
        {
            managerPanelView.managerName.Text = user.worker.name + " " + user.worker.surname;
        }

        public void clean() {
            if (managerPanelView != null) managerPanelView.Dispose();
            if (managerAddSalaryView != null) managerAddSalaryView.Dispose();
        }
    }
}
